package ltd.dujiabao.springcloud.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : DuJiabao
 * @Project : springcloud
 * @Time : 2021/5/22 20:44
 * @Desc :
 */

@Service
public class CircuitBreakerService {
  @HystrixCommand(fallbackMethod = "circuitBreakerFallback", commandProperties = {
          @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
          @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
          @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
          @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
  })
  public String paymentCircuitBreaker(Integer id) {
    if (id < 0) {
      throw new RuntimeException("******id 不能负数");
    }
    String serialNumber = IdUtil.simpleUUID();
    return Thread.currentThread().getName() + "\t" + "调用成功，流水号: " + serialNumber;
  }

  public String circuitBreakerFallback(Integer id) {
    return "当前服务已熔断，稍后再试，id：" + id;
  }
}
